package com.gewu.pm.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.ApprovalFlowBean;
import com.gewu.pm.bean.ReceiptInfoBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.adapter.order.ApprovalFlowAdapter;
import com.gewu.pm.ui.adapter.order.ReceiptInfoAdapter;
import com.gewu.pm.ui.adapter.order.VoucherImageAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 电汇收款详情页面
 */
public final class TelegraphicDetailsActivity extends AppActivity
        implements BaseAdapter.OnItemClickListener {

    private TextView tvTerminalName;
    private TextView tvStatus;
    private RecyclerView rvReceiptInfo;
    private TextView tvOrderNo;
    private TextView tvPaymentMethod;
    private TextView tvAmount;
    private RecyclerView rvVoucherImages;
    private RecyclerView rvApprovalFlow;
    private TextView btnEdit;

    private ReceiptInfoAdapter receiptInfoAdapter;
    private VoucherImageAdapter voucherImageAdapter;
    private ApprovalFlowAdapter approvalFlowAdapter;

    /**
     * @param id 应该是id  列表跳转到详情，需要用id来请求接口  目前暂时传过来的
     */
    public static void start(Context context, String id) {
        Intent intent = new Intent(context, TelegraphicDetailsActivity.class);
        intent.putExtra(IntentKey.ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.telegraphic_details_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTerminalName = findViewById(R.id.tv_terminal_name);
        tvStatus = findViewById(R.id.tv_status);
        rvReceiptInfo = findViewById(R.id.rv_receipt_info);
        tvOrderNo = findViewById(R.id.tv_order_no);
        tvPaymentMethod = findViewById(R.id.tv_payment_method);
        tvAmount = findViewById(R.id.tv_amount);
        rvVoucherImages = findViewById(R.id.rv_voucher_images);
        rvApprovalFlow = findViewById(R.id.rv_approval_flow);
        btnEdit = findViewById(R.id.btn_edit);

        // 设置收款信息适配器
        receiptInfoAdapter = new ReceiptInfoAdapter(this);
        rvReceiptInfo.setLayoutManager(new LinearLayoutManager(this));
        rvReceiptInfo.setAdapter(receiptInfoAdapter);

        // 设置收款凭证图适配器（3列网格）
        voucherImageAdapter = new VoucherImageAdapter(this);
        voucherImageAdapter.setOnItemClickListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        rvVoucherImages.setLayoutManager(gridLayoutManager);
        rvVoucherImages.setAdapter(voucherImageAdapter);

        // 设置审批流程适配器
        approvalFlowAdapter = new ApprovalFlowAdapter(this);
        rvApprovalFlow.setLayoutManager(new LinearLayoutManager(this));
        rvApprovalFlow.setAdapter(approvalFlowAdapter);

        // 设置点击事件
        setOnClickListener(btnEdit);
    }

    @Override
    protected void initData() {
        String id = getString(IntentKey.ID);
        // 加载模拟数据
        loadMockData();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == btnEdit) {// 点击编辑按钮
            // TODO: 跳转到编辑页面或显示编辑弹窗
            // 暂时跳转到收款登记页面    两个页面几乎一样，看后面后台给的什么数据
            PaymentRegistrationActivity.start(getContext());
        }
    }

    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        // 终端名称和状态
        tvTerminalName.setText("杭州配送站点A");
        tvStatus.setText("待审批");

        // 电汇收款信息
        List<ReceiptInfoBean> infoList = new ArrayList<>();
        infoList.add(new ReceiptInfoBean("收款单号：", "DH202508130003"));
        infoList.add(new ReceiptInfoBean("终端编码：", "POS00011820"));
        infoList.add(new ReceiptInfoBean("收款日期：", "2025-8-20"));
        infoList.add(new ReceiptInfoBean("车辆编码：", "6789580"));
        infoList.add(new ReceiptInfoBean("经办人：", "电汇配送商"));
        infoList.add(new ReceiptInfoBean("提交时间：", "2025-8-20"));
        receiptInfoAdapter.setData(infoList);

        // 本次收款信息
        tvOrderNo.setText("DH202508201234");
        tvPaymentMethod.setText("电汇");
        tvAmount.setText("¥5000");

        // 收款凭证图（使用网络图片占位，2张）
        List<Integer> imageList = new ArrayList<>();
        imageList.add(R.drawable.logo_big_ic);
        imageList.add(R.mipmap.logo_big_icon);
        voucherImageAdapter.setData(imageList);

        // 审批流程
        List<ApprovalFlowBean> flowList = new ArrayList<>();
        flowList.add(new ApprovalFlowBean("2025/8/20 14:30", "张三·已录入"));
        flowList.add(new ApprovalFlowBean("2025/8/20 15:00", "李四·已通过"));
        flowList.add(new ApprovalFlowBean("2025/8/20 16:00", "王五·审批中"));
        approvalFlowAdapter.setData(flowList);
    }

    /**
     * {@link BaseAdapter.OnItemClickListener}
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        if (recyclerView == rvVoucherImages) {
            toast("点击了凭证图片 " + (position + 1));
            // TODO: 显示大图预览
        }
    }
}

